package com.itheima.prize.api.action;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.prize.commons.config.RedisKeys;
import com.itheima.prize.commons.db.entity.CardUser;
import com.itheima.prize.commons.db.mapper.CardUserMapper;
import com.itheima.prize.commons.db.service.CardUserService;
import com.itheima.prize.commons.utils.ApiResult;
import com.itheima.prize.commons.utils.PasswordUtil;
import com.itheima.prize.commons.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping(value = "/api")
@Api(tags = {"登录模块"})
public class LoginController {
    @Autowired
    private CardUserService userService;

    @Autowired
    private RedisUtil redisUtil;

    @PostMapping("/login")
    @ApiOperation(value = "登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name="account",value = "用户名",required = true),
            @ApiImplicitParam(name="password",value = "密码",required = true)
    })
    public ApiResult login(HttpServletRequest request, @RequestParam String account,@RequestParam String password) {
        String guoqiKey = "guoqi" + ":"+account;
        String cishuKey = "cishu" + ":"+account;
        CardUser user = new CardUser();

        if (redisUtil.hasKey(guoqiKey)){
            return new ApiResult<>(0,"密码错误5次，请5分钟后再登录", null);
        }

        user = userService.query().eq("uname", account).eq("passwd", PasswordUtil.encodePassword(password)).one();
        if (user == null){
            long incr = redisUtil.incr(cishuKey, 1);
            if (incr >= 5){
                redisUtil.incr(guoqiKey,1);
                redisUtil.expire(guoqiKey,300);
            }
            return new ApiResult<>(0,"账户名或密码错误", null);
        }

        redisUtil.del(cishuKey);
        request.getSession().setAttribute("user", user);
        return new ApiResult<>(1,"登录成功", user);

    }

    @GetMapping("/logout")
    @ApiOperation(value = "退出")
    public ApiResult logout(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if (session != null) {
            session.invalidate();
        }
        return new ApiResult<>(1,"退出成功", null);
    }

}